// MCP 测试客户端
const axios = require('axios');

const SERVER_URL = 'http://localhost:3001';
const CLIENT_ID = `test-client-${Math.floor(Math.random() * 1000)}`;

// 简单的客户端代码
class McpClient {
  constructor(serverUrl, clientId) {
    this.serverUrl = serverUrl;
    this.clientId = clientId;
    this.connected = false;
    this.heartbeatInterval = null;
  }

  // 连接到服务器
  async connect() {
    try {
      console.log(`正在连接到 MCP 服务器: ${this.serverUrl}`);
      
      const response = await axios.post(`${this.serverUrl}/mcp/connect`, {
        clientId: this.clientId,
        clientVersion: '1.0.0'
      });
      
      if (response.data.status === 'connected') {
        this.connected = true;
        console.log(`连接成功! 服务器ID: ${response.data.serverId}`);
        
        // 开始发送心跳
        this.startHeartbeat();
        return true;
      } else {
        console.error('连接失败:', response.data);
        return false;
      }
    } catch (error) {
      console.error('连接错误:', error.message);
      return false;
    }
  }
  
  // 断开连接
  async disconnect() {
    if (!this.connected) {
      console.log('客户端未连接');
      return;
    }
    
    try {
      // 停止心跳
      this.stopHeartbeat();
      
      const response = await axios.post(`${this.serverUrl}/mcp/disconnect`, {
        clientId: this.clientId
      });
      
      this.connected = false;
      console.log('已断开连接:', response.data.message);
    } catch (error) {
      console.error('断开连接错误:', error.message);
    }
  }
  
  // 开始发送心跳
  startHeartbeat() {
    this.heartbeatInterval = setInterval(async () => {
      try {
        if (!this.connected) {
          this.stopHeartbeat();
          return;
        }
        
        const response = await axios.post(`${this.serverUrl}/mcp/heartbeat`, {
          clientId: this.clientId
        });
        
        console.log(`心跳响应: ${response.data.status} [${response.data.timestamp}]`);
      } catch (error) {
        console.error('心跳错误:', error.message);
      }
    }, 5000); // 每5秒发送一次心跳
  }
  
  // 停止心跳
  stopHeartbeat() {
    if (this.heartbeatInterval) {
      clearInterval(this.heartbeatInterval);
      this.heartbeatInterval = null;
    }
  }
  
  // 检查服务器状态
  async checkServerStatus() {
    try {
      const response = await axios.get(`${this.serverUrl}/mcp/status`);
      console.log('服务器状态:', response.data);
      return response.data;
    } catch (error) {
      console.error('获取服务器状态失败:', error.message);
      return null;
    }
  }
}

// 主函数
async function main() {
  const client = new McpClient(SERVER_URL, CLIENT_ID);
  
  console.log(`MCP测试客户端 (ID: ${CLIENT_ID})`);
  
  // 检查服务器状态
  await client.checkServerStatus();
  
  // 连接到服务器
  const connected = await client.connect();
  
  if (connected) {
    // 20秒后断开连接
    setTimeout(async () => {
      await client.disconnect();
      process.exit(0);
    }, 20000);
  } else {
    console.error('无法连接到服务器，退出');
    process.exit(1);
  }
}

// 启动客户端
main().catch(error => {
  console.error('客户端错误:', error);
  process.exit(1);
}); 